package com.bio.service.common.impl;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;
import org.springframework.transaction.annotation.Transactional;

import com.bio.service.common.CommonService;

@Transactional
public class CommonServiceImpl implements CommonService{

	@Override
	public JFreeChart getBarChart(String title, String horizontalAxis, String verticalAxis, CategoryDataset data) {
		JFreeChart chart = ChartFactory.createBarChart3D(title, // 图形标题名称
				horizontalAxis,// domain 轴 Lable这里先简单理解为横坐标Lable好了
				verticalAxis, // range 轴 Lable这里也先简单理解为纵坐标Lable好了
				data, // dataset
				PlotOrientation.VERTICAL, // 垂直显示
				true, // legend?
				true, // tooltips?
				false); // URLs?
		
		CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
		categoryplot.setBackgroundPaint(Color.LIGHT_GRAY); // 设定图表数据显示部分背景色
		categoryplot.setDomainGridlinePaint(Color.white); // 横坐标网格线白色
		categoryplot.setDomainGridlinesVisible(true); // 可见
		categoryplot.setRangeGridlinePaint(Color.white); // 纵坐标网格线白色
		// 设置series1与category的显示位置；类似效果图http://www.chinabs.net/webimages/PieChart7.png
		categoryplot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		categoryplot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

		categoryplot.getDomainAxis().setAxisLineVisible(false);

		// 使纵坐标的最小单位格为整数
		NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		BarRenderer barrenderer = (BarRenderer) categoryplot.getRenderer(); // 获得renderer
		// 以下设置，将按照指定格式，制定内容显示每个柱的数值。可以显示柱名称，所占百分比
		// barrenderer.setBaseItemLabelGenerator(new
		// StandardCategoryItemLabelGenerator("{2}",new DecimalFormat("0.0%")));
		// // 显示每个柱的数值，并修改该数值的字体属性
		// barrenderer.setBaseItemLabelGenerator(new
		// StandardCategoryItemLabelGenerator());
		// 设置柱的数值可见
		// barrenderer.setBaseItemLabelsVisible(true);

		// 设置每个地区所包含的平行柱的之间距离
		barrenderer.setItemMargin(0);
		// 显示每个柱的数值，并修改该数值的字体属性;类似效果图http://www.chinabs.net/webimages/PieChart6.png
		barrenderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		barrenderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 12));
		barrenderer.setItemLabelsVisible(true);

		// 默认的数字显示在柱子中，通过如下两句可调整数字的显示 // 注意这里是下嗍造型到BarRenderer！！
		// 注意：此句很关键，若无此句，那数字的显示会被覆盖，给人数字没有显示出来的问题
		barrenderer.setPositiveItemLabelPositionFallback(new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		// barrenderer.setItemLabelAnchorOffset(1);
		// // Set position of Items label : center
		// barrenderer.setPositiveItemLabelPosition(new
		// ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
		// barrenderer.setNegativeItemLabelPosition(new
		// ItemLabelPosition(ItemLabelAnchor.CENTER, TextAnchor.CENTER));
		// // If there isn't enough space to draw the ItemLabel...
		// barrenderer.setPositiveItemLabelPositionFallback(new
		// ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER));
		// barrenderer.setNegativeItemLabelPositionFallback(new
		// ItemLabelPosition(ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER));

		// //所有柱子一个连接，注意链接地址 从http开始
		// barrenderer.setItemURLGenerator(new
		// StandardCategoryURLGenerator("http://www.baidu.com"));
		// //设置url: 参数一是第n个柱子,会在连接后自动添加参数series=红色的柱子&category=0
		// (即series=柱组名rowKeys&category=柱数)
		// barrenderer.setSeriesItemURLGenerator(0, new
		// StandardCategoryURLGenerator("http://www.goole.com"));
		// barrenderer.setSeriesItemURLGenerator(1, new
		// StandardCategoryURLGenerator("http://www.baidu.com"));
		// barrenderer.setSeriesItemURLGenerator(2, new
		// StandardCategoryURLGenerator("http://www.sohu.com"));

		barrenderer.setDrawBarOutline(true); // Bar的外轮廓线
		// 设置柱的透明度
		categoryplot.setForegroundAlpha(0.6f);
		categoryplot.setRenderer(barrenderer);
		// GradientPaint gradientpaint = new GradientPaint(0.0F, 0.0F,
		// Color.blue,0.0F, 0.0F, new Color(20, 20, 64)); //设定特定颜色
		// GradientPaint gradientpaint1 = new GradientPaint(0.0F, 0.0F,
		// Color.green,0.0F, 0.0F, new Color(20, 64, 20));
		// GradientPaint gradientpaint2 = new GradientPaint(0.0F, 0.0F,
		// Color.red,0.0F, 0.0F, new Color(64, 20, 20));
		// barrenderer.setSeriesPaint(0, gradientpaint); //给series1 Bar设定上面定义的颜色
		// barrenderer.setSeriesPaint(1, gradientpaint1); //给series2 Bar
		// 设定上面定义的颜色
		// barrenderer.setSeriesPaint(2, gradientpaint2); //给series3 Bar
		// 设定上面定义的颜色
		// CategoryAxis categoryaxis = categoryplot.getDomainAxis(); //横轴上的
		// Lable 45度倾斜
		// categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
		//
		// IntervalMarker intervalmarker = new IntervalMarker(4.5D, 7.5D);
		// intervalmarker.setLabel("Target Range");
		// intervalmarker.setLabelFont(new Font("SansSerif", 2, 11));
		// intervalmarker.setLabelAnchor(RectangleAnchor.LEFT);
		// intervalmarker.setLabelTextAnchor(TextAnchor.CENTER_LEFT);
		// intervalmarker.setPaint(new Color(222, 222, 255, 128));
		// categoryplot.addRangeMarker(intervalmarker, Layer.BACKGROUND);
		
		//解决中文乱码
		chart.getTitle().setFont(new Font("宋体", Font.BOLD,16));
		Font font = new Font("宋体",Font.PLAIN,12);
		chart.getLegend().setItemFont(font);
		CategoryPlot plot = chart.getCategoryPlot();// 获得图表区域对象
		// 设置图表的纵轴和横轴
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setTickLabelFont(font);
		domainAxis.setLabelFont(font);
		numberaxis.setTickLabelFont(font);
		numberaxis.setLabelFont(font);
		return chart;
	}

	@Override
	public JFreeChart getPieChart(String title, DefaultPieDataset data) {
		JFreeChart chart = ChartFactory.createPieChart3D(title, data, true, true, false);
		
		//设置显示时，饼图数据带上数据的百分比
	    //("{0}: ({1}，{2})")是生成的格式，{0}表示section名，{1}表示section的值，{2}表示百分比。
	    //而new DecimalFormat("0.00%")表示小数点后保留两位。
		PiePlot plot = (PiePlot) chart.getPlot();
		plot.setNoDataMessage("无数据"); // 没有数据的时候显示的内容
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
			     ("{0}: {1}({2})"), NumberFormat.getNumberInstance(),
			     new DecimalFormat("0.00%")));
		
		//解决中文乱码
		chart.getTitle().setFont(new Font("宋体", Font.BOLD,16));
		PiePlot pieplot = (PiePlot) chart.getPlot(); 
		pieplot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		return chart;
	}

	@Override
	public JFreeChart getLineChart(String title, String horizontalAxis,
			String verticalAxis, DefaultCategoryDataset data) {
		//定义图表对象 
		JFreeChart chart = ChartFactory.createLineChart(title, // chart title 
		  horizontalAxis, // domain axis label 
		  verticalAxis, // range axis label 
		  data, // data 
		  PlotOrientation.VERTICAL, // orientation 
		  true, // include legend 
		  true, // tooltips 
		  false // urls 
		); 
		CategoryPlot plot = chart.getCategoryPlot(); 
		// customise the range axis... 
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
		rangeAxis.setAutoRangeIncludesZero(true); 
		rangeAxis.setUpperMargin(0.20); 
		rangeAxis.setLabelAngle(Math.PI / 100.0); 
		//chart.setBackgroundPaint(Color.GREEN);//设置曲线图背景色
		
		//中文乱码
		chart.getTitle().setFont(new Font("宋体", Font.BOLD,16));
		Font font = new Font("宋体",Font.PLAIN,12);
		chart.getLegend().setItemFont(font);
		rangeAxis.setTickLabelFont(font);
		rangeAxis.setLabelFont(font);
		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setTickLabelFont(font);
		domainAxis.setLabelFont(font);
		return chart; 
	}
}
